package Tian7
{
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;

	/**
	 * 数据 
	 * @author Kakera
	 * 
	 */
	public class DataProvider extends URLLoader
	{
		public var result:XML;
		public function DataProvider() {
			dataFormat = URLLoaderDataFormat.TEXT;
		}
		
		public override function load(request:URLRequest):void {
			addEventListener(Event.COMPLETE, completeHandler, false, int.MAX_VALUE);
			super.load(request);
		}
		
		private function completeHandler (event:Event):void {
			event.stopImmediatePropagation();
			
			// 清 XML 声明, 重新格式化 -V-, 洁癖
			result = XML(String(data).replace(/\<\?.+\?\>/, "")
					.replace(/WeatherDataSet/g, "root")
					.replace(/WeatherDS/g, "node")
					.replace(/CityName/g, "city")
					.replace(/Date(\d)/g, "date$1")
					.replace(/Weather(\d)/g, "weather$1")
					.replace(/Temp(\d)/g, "temp$1")
					.replace(/WindPower(\d)/g, "wind$1"));
			
			removeEventListener(Event.COMPLETE, completeHandler);
			dispatchEvent(event);
		}
	}
}